/**
 * Zhznet.com Inc.
 * Copyright (c) 2004-2011 All Rights Reserved.
 */
package com.liwei.httpclient.demo;

import java.io.IOException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * 提交参数演示
 * 该程序连接到一个用于查询手机号码所属地的页面
 * 以便查询号码段1330227所在的省份以及城市
 *
 * @author Liwei
 * @version $Id: SimpleHttpClient.java, v 0.1 2011-1-22 上午10:06:29 Liwei Exp $
 */
public class SimpleHttpClient {
    public static void main(String[] args) throws IOException {
        HttpClient client = new HttpClient();
        client.getHostConfiguration().setHost("www.imobile.com.cn", 80, "http");
        HttpMethod method = getPostMethod(); // 使用 POST 方式提交数据   
        client.executeMethod(method); //打印服务器返回的状态   
        System.out.println(method.getStatusLine()); //打印结果页面
        String response = new String(method.getResponseBodyAsString().getBytes("UTF-8"));

        client.executeMethod(method);
        System.out.println(method.getStatusLine().toString());
        method.releaseConnection();
        // 检查是否重定向
        int statuscode = method.getStatusCode();
        if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
            || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
            || (statuscode == HttpStatus.SC_SEE_OTHER)
            || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
            // 读取新的 URL 地址   
            Header header = method.getResponseHeader("location");
            if (header != null) {
                String newuri = header.getValue();
                if ((newuri == null) || (newuri.equals("")))
                    newuri = "/";
                GetMethod redirect = new GetMethod(newuri);
                client.executeMethod(redirect);
                System.out.println("Redirect:" + redirect.getStatusLine().toString());
                response = new String(redirect.getResponseBodyAsString().getBytes("UTF-8"));

                //打印返回的信息
                System.out.println(response);

                redirect.releaseConnection();
            } else
                System.out.println("Invalid redirect");

        }
        method.releaseConnection();

    }

    /**   
      * 使用 GET 方式提交数据   
    *@return   
    */

    private static HttpMethod getGetMethod() {
        return new GetMethod("/simcard.php   simcard=1330227");
    }

    /**   
       * 使用 POST 方式提交数据   
       *@return   
       */
    private static HttpMethod getPostMethod() {
        PostMethod post = new PostMethod("/simcard.php");
        NameValuePair simcard = new NameValuePair("simcard", "1330227");
        post.setRequestBody(new NameValuePair[] { simcard });
        return post;

    }
}
